import routerConfig from "@/common/routerConfig.js"
const {
	visitor,
	loginPattern,
	needBindDms,
} = routerConfig //需要登录的页面


module.exports = {
	onLoad() {
		// this.checkPage()
	},
	methods: {
		// 扫码进入该页面需要校验登录
		checkScanPage(params={}, coverRouterOption) {
			const wxDetail = this.userInfo.detail.status,
				isBindDms = this.isBindDms,
				pages = getCurrentPages(),
				isScanIn = pages.length == 1

			if (isScanIn) {
				const currentUrl = pages[0].route
				const fromOptions = pages[0].options

				let pass = false
				let dmsPass = true
				if (visitor) {
					pass = visitor.some((item) => {
						if (typeof(item) == 'object' && item.pattern) {
							return item.pattern.test(currentUrl)
						}
						return currentUrl == item
					})
				}
				if (needBindDms) {
					dmsPass = needBindDms.every(item => {
						if (typeof(item) == 'object' && item.pattern) {
							return !item.pattern.test(currentUrl)
						}
						return currentUrl != item
					})
				}
				
				if (!pass && wxDetail == 0) {
					let routeParams = {
						...params
					}
					if (!coverRouterOption) {
						Object.assign(routeParams, fromOptions)
					}
					this.$u.route({
						url: '/pages/login/login',
						params: {
							fromUrl: currentUrl,
							...routeParams,
						},
						type: 'redirect'
					})
					return false
				}

				if (!dmsPass && !isBindDms) {
					this.$u.route({
						url: '/pages/login/bindDms',
						params: {
							fromUrl: `${currentUrl}${uni.$u.queryParams(fromOptions)}`,
						}
					})
					return false
				}

			}
		}
	}
}
